// c:\Users\Administrator\Desktop\hahah\toutiao\pages\my\my.js
Page({
  data: {
    geren: '',
    list: [],
    userinfo: '',
    xuanzhong: true,
    token: '',
    nickName:"",
    userids:'0'
    
  },
  onLoad: function (options) {
    this.logininfo()
    // tt.login({
    //   success(res) {
    //     console.log("今日")
    //     console.log(res)
    //     var code = res.code;

    //   }
    // })

   

  },
  onShow: function () {
    var that=this
    // tt.setStorageSync('token',"")
    this.newlogin()
 
   if(tt.getStorageSync("uid")==""||tt.getStorageSync("uid")==undefined||tt.getStorageSync("uid")==null){
    this.setData({
      userids:"0"
     })
   }else{
    this.setData({
      userids:"1"
     })
     that.loginarr()
    //  this.logininfo()
   }
   
    console.log(this.data.userids)
  },

  loginarr(){
    var that=this
    const user = tt.getStorageSync('meinv')
    tt.request({
      url:'https://ceshi666.yxbnet.cn/api/Ttlogin/seluserinfo',
      data:{
        userid:user.user_id
      },
      method:'POST',
      success: res => {
        console.log(543534, res);
        that.setData({
          nickName:res.data.data.nickName,
          avatarUrl:res.data.data.avatarUrl

          // geren: res.data.data
        })
      }

    })
  },
newlogin(){
  const user1 = tt.getStorageSync("uid")
  const token = tt.getStorageSync('token')
  if(token==""||token==undefined||token==null){
    this.data.token=""
  }else{
    this.data.token=token
  }
  // const lala = tt.getStorageSync('lala')
  // 投票记录

  tt.request({
    url: 'https://ceshi666.yxbnet.cn/api/Personal/vote',
    data: {
      userid: user1
    },
    method: 'POST',
    success: res => {
      this.setData({
        list: res.data.data
      })
    }
  })
  this.setData({
    token: token
  })
},
  logininfo() {
    var that=this
    tt.login({
      success(res) {
        var code = res.code;
        if (res.code) {
          tt.request({
            url: 'https://ceshi666.yxbnet.cn/api/Ttlogin/ttLogin',
            data: {
              code: code
            },
            method: 'POST',
            success: res => {
              console.log(44, res);
              const token = res.data.data.userinfo.token
              tt.setStorageSync("uid", res.data.data.userinfo.user_id)
              const meinv=res.data.data.userinfo
              tt.setStorageSync("token", token)
              tt.setStorageSync("meinv", meinv)
            
            }
          })
        }
      }
    })
  },
  qufan() {
    this.setData({
      xuanzhong: !this.data.xuanzhong
    })
  },
  goxiangqing(e) {
    const id = e.currentTarget.dataset.id;
    tt.navigateTo({
      url: `/pages/jieguo/jieguo?id=${id}`
    })
  },
  denglu() {
    let that = this
    const token = wx.getStorageSync('token');
      tt.getUserProfile({
        success: (res) => {
          console.log("允许")
          var newarr=res
          setTimeout(function(){
            console.log(888, res);
            const meinv = newarr.userInfo
            console.log(999, meinv);
            tt.setStorageSync("lala", meinv)
            console.log(newarr)
            console.log(tt.getStorageSync("uid"))
            tt.request({
              url: 'https://ceshi666.yxbnet.cn/api/Ttlogin/userinfo',
              data: {
                userid:  tt.getStorageSync("uid"),
                nickname:newarr.userInfo.nickName,
                avatar:newarr.userInfo.avatarUrl
              },
              method: 'POST',
              success: res => {
                console.log("成功")
                console.log(res)
                that.loginarr()

              }
            })
            that.setData({
              geren: meinv,
              userids:"1"
            })
            that.newlogin()
          },1000)
         
        }
      });
 
  },



// 广告
  adloadhandler(e) {
    console.log("广告加载成功");
  },
  aderrorhandler(e) {
    console.log("广告加载失败", e);
    tt.showToast({
      title: "广告加载失败" + e.errMsg,
      icon: "fail",
    });
  },
  adclosehandler(e) {
    console.log("广告关闭");
  },
})